package top.healthylife.jobs;/*
 *@Author GaoZeXi
 *@Created time 2020/4/10 14:48
 *@Description:
 * Step by Step  and Stand on,  You Are The Best Investment!!!
 */

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileOutputStream;
import java.io.IOException;

public class HandleExcel {
    public static void main(String[] args) {
//        readXlsx();
//        readXlsx2();
        writeExcel();
    }

    private static void readXlsx() {
        try {
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook("C:\\Users\\12008\\Desktop\\cookie-session.xlsx");
            XSSFSheet sheet0 = xssfWorkbook.getSheetAt(0);
            for (Row row : sheet0) {
                for (Cell cell : row) {
                    System.out.println(cell.getStringCellValue());
                }
            }
            xssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readXlsx2() {
        try {
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook("C:\\Users\\12008\\Desktop\\cookie-session.xlsx");
            XSSFSheet sheet0 = xssfWorkbook.getSheetAt(0);
            for (int i = 0; i <=sheet0.getLastRowNum() ; i++) {
                XSSFRow row = sheet0.getRow(i);
                int lastCellNum=row.getLastCellNum();
                for (int j = 0; j <lastCellNum ; j++) {
                    XSSFCell cell = row.getCell(j);
                    if(cell!=null){
                        System.out.println("单元格" +(i+1)+"行"+(j+1)+"列"+cell.getStringCellValue());
                    }else {
                        System.out.println("单元格" +(i+1)+"行"+(j+1)+"列"+"");

                        continue;
                    }

                }
            }
            xssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void writeExcel(){
        XSSFWorkbook sheets = null;
        try {
            sheets = new XSSFWorkbook();
            XSSFSheet sheetAt = sheets.createSheet("poi测试");
            for (int i = 0; i < 10; i++) {
                XSSFRow row = sheetAt.createRow(i);
                for (int j = 0; j <10 ; j++) {
                    if(j%2==0){
                        row.createCell(j).setCellValue("姓名:小明"+j);
                    }else {
                        row.createCell(j).setCellValue("学号:"+j);
                    }

                }
            }
            sheets.write(new FileOutputStream("C:\\Users\\12008\\Desktop\\cookie-session2.xlsx"));
            System.out.println("写入完成");
            sheets.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
